﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;


namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration
{
    [TypeConverter(typeof(BookMargins.ExpressionConverter))]
    [Serializable]
    public class BookMargins
    {
        private const float defaultLeft = 60f;
        private const float defaultRight = 42f;
        private const float defaultTop = 50f;
        private const float defaultBottom = 50f;

        #region Internal TypeConverter
        public class ExpressionConverter : TypeConverter
        {

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
                object value, Attribute[] attributes)
            {
                PropertyDescriptorCollection properties =
                    TypeDescriptor.GetProperties(value, attributes);

                // Changing the display order
                string[] textOrder = new string[properties.Count];
                textOrder[0] = "Top";
                textOrder[1] = "Bottom";
                textOrder[2] = "Left";
                textOrder[3] = "Right";
                // Return a sorted list of properties
                return properties.Sort(textOrder);
            }
        }
        #endregion //Internal TypeConverter



        public BookMargins()
        {
            _left = defaultLeft;
            _top = defaultTop;
            _right = defaultRight;
            _bottom = defaultBottom;
        }

        private float _left;
        [DefaultValue(defaultLeft)]
      //  [Category("Margins")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DescriptionAttribute("Defines the left margin used for Pdf document")]
        public float Left
        {
            get { return _left; }
            set {  _left = EnsureLimit(value, defaultLeft); }
        }

        private float _right;
        [DefaultValue(defaultRight)]
   //     [Category("Margins")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DescriptionAttribute("Defines the right margin used for Pdf document")]
        public float Right
        {
            get { return _right; }
            set { _right = EnsureLimit(value, defaultRight); }
        }

        private float _top;
        [DefaultValue(defaultTop)]
  //      [Category("Margins")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DescriptionAttribute("Defines the top margin used for Pdf document")]
        public float Top
        {
            get { return _top; }
            set { _top = EnsureLimit(value, defaultTop); }
        }

        private float _bottom;
        [DefaultValue(defaultBottom)]
  //      [Category("Margins")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DescriptionAttribute("Defines the bottom margin used for Pdf document")]
        public float Bottom
        {
            get { return _bottom; }
            set { _bottom = EnsureLimit(value, defaultBottom); }
        }

        private float EnsureLimit(float val, float defaultVal)
        {
            if (val < 0.0 || val > 100.0)
                val = defaultVal;
            return val;
        }

        public override string ToString()
        {
            return Top.ToString() + ";" + Bottom.ToString() + ";" + Left.ToString() + ";" + Right.ToString();
        }
    }
}
